// pages/contentus/index.js
const {
  BASEURL
} = require('../../utils/baseurl')
const { fetch, newFetch } = require('../../utils/fetch')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadList: [],
    uploadimgs: [],
    details: '',
    contactPhone: '',
    pushPhone: '18510939755',
    pushwechat: 'weixin8888',
    specificarea:null,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea:wx.getStorageSync('specificarea')
      })
    }
    this.initData()
  },

  initData: function() {
    var that = this
    fetch(`wx/memberagent/${this.data.specificarea}/areaContact`, 'POST').then(res => {
      if(res) {
        console.log(res, '哈哈哈')
        that.setData({
          pushPhone:res.phone,
          pushwechat:res.wxname
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // 输入框的描述值
  goinputdetail: function (e) {
    this.setData({
      details: e.detail.value
    })
  },
  
  // 输入框的电话值
  goinputphone: function (e) {
    this.setData({
      contactPhone: e.detail.value
    })
  },
  
  // 上传图片  九图
  uploadimg() {
    const _this = this
    let {
      uploadimgs
    } = this.data
    var resupimg = []

    wx.chooseImage({
      count: 4 - uploadimgs.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        res.tempFilePaths.forEach((item, index) => {
          wx.uploadFile({
            url: `${BASEURL}wx/upload/image`,
            filePath: item,
            name: 'image',
            formData: {
              type: 'img'
            },
            success: (res) => {
              var resdata = JSON.parse(res.data);
              if (resdata.code == '200') {
                resupimg[index] = resdata.data.url
                this.setData({
                  uploadimgs: resupimg
                })
                _this.setData({
                  tip: '显示',
                  show_tip: 0
                })
              } else if (resdata.code == '404') {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            },
            fail: function (res) {
              console.log(res, 'fail')
            }
          })
        })
      }
    })
  },
  previewImagetwo(e) {
    let index = e.target.dataset.index
    let {
      uploadList
    } = this.data
    wx.previewImage({
      current: uploadList[index],
      urls: uploadList
    })
  },
  removeimg(e) {
    const _this = this
    let index = e.target.dataset.index
    let {
      uploadimgs
    } = this.data

    this.data.uploadimgs.map((val, uploindex) => {
      if (uploindex == index) {
        wx.showModal({
          title: '提示',
          content: '您确定要删除本张图片吗？',
          success(res) {
            if (res.confirm) {
              uploadimgs.splice(index, 1)
              _this.setData({
                uploadimgs
              })
            } else if (res.cancel) {}
          }
        })
      }
    })
  },
  bind_tel(e) {
    wx.makePhoneCall({
      contactPhone: e.currentTarget.dataset['tel'],
    })
  },
  
  // 复制内容
  goinputCopy:function(e) {
    var details = e.currentTarget.dataset.details
    wx.setClipboardData({
      data: details,
      success: function (res) {
        // 添加下面的代码可以复写复制成功默认提示文本`内容已复制` 
        wx.showToast({
          title: '复制成功',
          duration: 1500
        })
        wx.getClipboardData({
          success: function (res) {
          }
        })
      }
    })
  },
  // 确认提交
  publishFN: function (e) {
    let userInfo = wx.getStorageSync('userInfo');
    if (!this.data.details) {
      wx.showToast({
        title: '请填写您的问题',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    if (!this.data.contactPhone || !/^1(3|4|5|7|8)\d{9}$/.test(this.data.contactPhone)) {
      wx.showToast({
        title: '请输入正确联系方式',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    var params = {
      content:this.data.details,
      imgs: this.data.uploadimgs,
      contact: this.data.contactPhone,
      memberid: userInfo.id,
      area: this.data.specificarea
    }
    newFetch(`wx/feedback/${this.data.specificarea}/save`, 'POST', params).then(res => {
      if(res) {
        // wx.reLaunch({
        //     url: '/pages/index/index?interactive='+'my'
        // })
        wx.redirectTo({
          url: '/pages/index/index?interactive='+'my'
       })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})